package com.party.admin.task;

import com.party.common.utils.BigDecimalUtils;
import com.party.common.utils.DateUtils;
import com.party.core.model.crowdfund.MemberWithdrawalFund;
import com.party.core.model.member.MemberGroupInfo;
import com.party.core.model.member.MerchantUtil;
import com.party.core.model.partner.MemberPartner;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.crowdfund.IMemberWithdrawalFundService;
import com.party.core.service.member.IMemberGroupInfoService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.partner.IMemberPartnerService;
import com.party.core.service.wallet.biz.WithdrawalsBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 机构提现数据维护
 * @Author: hqh
 * @Date: 2018/11/09 15:45
 */
@Component("quartzMemberWithdrawals")
public class QuartzMemberWithdrawals {
    @Autowired
    private IMemberWithdrawalFundService memberWithdrawalFundService;

    @Autowired
    private WithdrawalsBizService withdrawalsBizService;

    @Autowired
    private IMemberPartnerService memberPartnerService;

    @Autowired
    private IMemberGroupInfoService memberGroupInfoService;

    @Autowired
    private ICrowdfundEventService crowdfundEventService;

    @Autowired
    private IMemberGroupService memberGroupService;

    private static Logger logger = LoggerFactory.getLogger(QuartzMemberWithdrawals.class);

    /**
     * 刷新机构每个主题的可提现金额
     * ---每周一 01：00 运行一次
     */
    public void init(){
        logger.info("开始____机构提现数据维护");
        long staTime = System.currentTimeMillis();


        Map<String,Object> params = new HashMap<>();
        Set<String> merchants = MerchantUtil.getTxzMerchants();
        params.put("txzMerchantId", merchants);


        //上周日
        String lastDay = DateUtils.formatDate(DateUtils.getFirstDayOfWeek(0))+" 00:00:00";
        List<MemberWithdrawalFund> mewfLists =memberWithdrawalFundService.getMemberEventWithdrawalFund(lastDay,params);
        for (MemberWithdrawalFund mmp:mewfLists){

            Integer sucessNum =  memberWithdrawalFundService.countBySuccessDate(mmp.getBusinessId(),lastDay,0,params);
            mmp.setSuccessNum(sucessNum);

            Integer transferSuccessNum = memberWithdrawalFundService.countBySuccessDate(mmp.getBusinessId(),lastDay,1,params);
            mmp.setTransferSuccessNum(transferSuccessNum);

            //总的人数
            Integer totleNum =sucessNum+transferSuccessNum;
            //总可提现金额
            Double totalAleAmount = 0d;
            //服务费
            Double serviceCharge = 0d;

            String partnerId = memberGroupService.getPartnerId(mmp.getMemberId());
            MemberPartner memberPartner = memberPartnerService.findByMemberId(partnerId);

            if(memberPartner!=null){
                if(MemberPartner.PARTNER_TYPE_DEFAULT.equals(memberPartner.getPartnerType())){
                    //系统客户的主题服务费
                    serviceCharge =crowdfundEventService.get(mmp.getBusinessId()).getServiceCharge();

                }else{
                    MemberGroupInfo memberGroupInfo = memberGroupInfoService.findByMemberId(partnerId);
                    if (null != memberGroupInfo) {
                        //其他客户的服务费
                        serviceCharge= memberGroupInfo.getServiceCharge();
                    }
                }
            }

            serviceCharge = serviceCharge ==null ? 0d:serviceCharge;
            if(0d!=serviceCharge){
                serviceCharge = BigDecimalUtils.mul(totleNum,serviceCharge);
            }

            totalAleAmount =  BigDecimalUtils.sub(mmp.getTotalAleAmount(),serviceCharge);
            totalAleAmount = BigDecimalUtils.round(totalAleAmount, 2);
            mmp.setTotalAleAmount(totalAleAmount);
            withdrawalsBizService.sevaOrupdateMemberWithdrawalFund(mmp);
        }
        long endTime = System.currentTimeMillis()-staTime;
        logger.info("机构提现数据维护_____结束__"+"用时："+endTime);
    }

/*    *//**
     * 刷新机构每个主题的可提现金额————高端活动
     * ---每天 00：00 运行一次
     *//*
    public void init1(){
        logger.info("开始____机构提现数据维护——————————————————高端活动");
        long staTime = System.currentTimeMillis();
        //当天
        String veryDay = DateUtils.formatDate(new Date());
        List<MemberWithdrawalFund> mewfLists =memberWithdrawalFundService.getMemberEventWithdrawalFund(veryDay,1);
        for (MemberWithdrawalFund mmp:mewfLists){
            Integer sucessNum =  memberWithdrawalFundService.countBySuccessDate(mmp.getBusinessId(),veryDay);
            mmp.setSuccessNum(sucessNum);
            withdrawalsBizService.sevaOrupdateMemberWithdrawalFund(mmp);
        }
        long endTime = System.currentTimeMillis()-staTime;
        logger.info("机构提现数据维护_____结束———————————————————高端活动"+"用时："+endTime);
    }*/
}
